﻿using Common.Core.Extension;
using Common.Extension;
using System.Reflection;

namespace NetByDocker.Core.Npoi.Dto
{
    public class ColumnProperty
    {
        public ColumnProperty(PropertyInfo property)
        {
            Name = property.GetCustomAttribute(typeof(ColumnNameAttribute)) is ColumnNameAttribute nameAttr
                ? nameAttr.Name
                : property.Name;


            PropertyInfo = property;
        }

        /// <summary>
        /// 列名
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 列对应的对象属性
        /// </summary>
        public PropertyInfo PropertyInfo { get; set; }

        /// <summary>
        /// 获取该属性在cell中展示的文本
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
        public string GetCellValue(object target)
        {
            var objectValue = PropertyInfo.GetValue(target);

            var cellValue = string.Empty;
            if (objectValue != null)
            {
                if (PropertyInfo.PropertyType == typeof(DateTime) || PropertyInfo.PropertyType == typeof(DateTime?))
                {
                    cellValue = ((DateTime)objectValue).ToStandardString();
                }
                else
                {
                    cellValue = objectValue.ToString();
                }
            }

            return cellValue;
        }
    }
}